package com.module.app.widget.preview;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.module.app.bean.IType;
import com.module.app.bean.IType;
import com.module.base.cache.CacheSDK;
import com.module.base.glide.SelectorGlideEngine;
import com.jaeger.library.StatusBarUtil;
import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.config.PictureSelectionConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.style.PictureWindowAnimationStyle;
import com.module.base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片预览
 */
public class MyPictureExternalPreviewActivity extends PictureExternalPreviewActivity {

    public static final String EXTRA_IS_HORIZONTAL = "isHorizontal";

    /**
     * 单图
     *
     * @param width 用于判断是否是长图
     */
    public static void start(Activity context, String url, int width, int height, boolean isHorizontal) {
        List<LocalMedia> medias = new ArrayList<>();
        LocalMedia localMedia = LocalMedia.parseLocalMedia(url, 0, PictureMimeType.ofImage());
        localMedia.setWidth(width);
        localMedia.setHeight(height);
        medias.add(localMedia);
        MyPictureExternalPreviewActivity.start2(context, 0, medias, isHorizontal);
    }

    /**
     * 单图
     *
     * @param width 用于判断是否是长图
     */
    public static void start(Activity context, String url, int width, int height) {
        List<LocalMedia> medias = new ArrayList<>();
        LocalMedia localMedia = LocalMedia.parseLocalMedia(url, 0, PictureMimeType.ofImage());
        localMedia.setWidth(width);
        localMedia.setHeight(height);
        medias.add(localMedia);
        MyPictureExternalPreviewActivity.start2(context, 0, medias, false);
    }

    /**
     * 多图
     */
    public static void start(Activity context, int position, List<String> list) {
        List<LocalMedia> medias = new ArrayList<>();
        for (String url : list) {
            LocalMedia localMedia = LocalMedia.parseLocalMedia(url, 0, PictureMimeType.ofImage());
            medias.add(localMedia);
        }
        MyPictureExternalPreviewActivity.start2(context, position, medias, false);
    }


    public static void start2(Activity context, int position, List<LocalMedia> medias, boolean isHorizontal) {
        PictureSelector.create(context)
                .themeStyle(R.style.picture_default_style)
                .isNotPreviewDownload(false)
                .setPictureWindowAnimationStyle(PictureWindowAnimationStyle.ofCustomWindowAnimationStyle(R.anim.alpha_anim_in, R.anim.alpha_anim_out))
                .imageEngine(SelectorGlideEngine.createGlideEngine());

        Intent intent = new Intent(context, MyPictureExternalPreviewActivity.class);
        intent.putParcelableArrayListExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST,
                (ArrayList<? extends Parcelable>) medias);
        intent.putExtra(PictureConfig.EXTRA_POSITION, position);
        intent.putExtra(EXTRA_IS_HORIZONTAL, isHorizontal);
        context.startActivity(intent);
        if (!isHorizontal) {
            context.overridePendingTransition(R.anim.alpha_anim_in, R.anim.alpha_anim_out);
        }
    }


    private View mMyTitleBar;
    private ImageView iv_back;
    private boolean isRealFinish;
    private boolean isHorizontal;

    @Override
    public int getResourceId() {
        return R.layout.activity_picture_external_preview;
    }

    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);//状态栏透明
        StatusBarUtil.setDarkMode(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        //任务列表显示空白，避免偷窥
        Boolean closeSecure = CacheSDK.get(IType.ICache.CLOSE_SECURE, Boolean.class);
        if (!closeSecure) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initWidgets() {
        isHorizontal = getIntent().getBooleanExtra(EXTRA_IS_HORIZONTAL, false);
        setStatusBar();
        super.initWidgets();
        mMyTitleBar = findViewById(R.id.titleBar);
        iv_back = findViewById(R.id.iv_back);
        mMyTitleBar.setVisibility(View.GONE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        if (isHorizontal) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    @Override
    public void finish() {
        if (!isRealFinish) {
            return;
        }
        super.finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        isRealFinish = true;
        finish();
        overridePendingTransition(R.anim.picture_anim_fade_in, PictureSelectionConfig.windowAnimationStyle.activityPreviewExitAnimation);
    }
}
